
#include <Modbus.h>
extern uint8_t pin_reg[40];


const signed char Modbus_byte_order[][4]={ {0,0,0,0},	
									 {2,2,-2,-2},
									 {1,-1,1,-1},
									 {3,1,-1,-3},
									 {0,0,0,0},
									 {1,-1,1,-1}
									}; 

unsigned short Modbus_CRC(unsigned char *Data,unsigned int len)
{
	unsigned short crc;
	unsigned char t,a,i,j;
	crc=0xffff;
	for(i=0;i<len;i++)
	{
		t=Data[i];
		crc=(crc&0xff00)|(unsigned short)((unsigned char)crc^t);
		for(j=0;j<8;j++)
		{
			a=(unsigned char)crc&0x01;
			crc>>=1;
			if(a)
			{
				crc=((crc^0xa000)&0xff00)|((crc^0x0001)&0x00ff);
			}
		}
	}
	return crc;
}
void Byte_order(unsigned char *DataSource,unsigned char *Data,unsigned short len,unsigned char Data_type)
{
	unsigned short i;
	for(i=0;i<len;i++)
	{
		Data[i+Modbus_byte_order[Data_type][i%4]]=DataSource[i];
	}
}
unsigned char computer_addr_head(Modbus_struct *Modbus,unsigned char **addr_head)
{
	unsigned char bit_num;
	unsigned short addr;
	addr=Modbus->Reg_addr_start;
	
 if((addr>=short_head_start)&&(addr<=short_head_end))//16int
	{
		bit_num=2;
		addr=addr-short_head_start;
		*addr_head=(unsigned char *)(&short_reg.head);
		*addr_head=addr*2+*addr_head;
	}
	else
	{
		bit_num=0;
		Modbus->Error=Modbus_error_reg_addr;
	}
	return bit_num;
	return 0;
}

/*
unsigned char computer_addr_head(Modbus_struct *Modbus,unsigned char **addr_head)
{
	unsigned char bit_num;
	if((Modbus->Reg_addr_start>=Float_r_reg_start)&&(Modbus->Reg_addr_start<=Float_r_reg_end))//float
	{
		bit_num=4;
		Modbus->Reg_addr_start=Modbus->Reg_addr_start-Float_r_reg_start;
		*addr_head=(unsigned char *)(&Float_r_reg.head);
		*addr_head=Modbus->Reg_addr_start*2+*addr_head+4;
	}
	else if((Modbus->Reg_addr_start>=LongInt_r_reg_start)&&(Modbus->Reg_addr_start<=LongInt_r_reg_end))//32int
	{
		bit_num=4;
		Modbus->Reg_addr_start=Modbus->Reg_addr_start-LongInt_r_reg_start;
		*addr_head=(unsigned char *)(&LongInt_r_reg.head);
		*addr_head=Modbus->Reg_addr_start*2+*addr_head+4;
	}
	else if((Modbus->Reg_addr_start>=Int_r_reg_start)&&(Modbus->Reg_addr_start<=Int_r_reg_end))//16int
	{
		bit_num=2;
		Modbus->Reg_addr_start=Modbus->Reg_addr_start-Int_r_reg_start;
		*addr_head=(unsigned char *)(&Int_r_reg.head);
		*addr_head=Modbus->Reg_addr_start*2+*addr_head+2;
	}
	else if((Modbus->Reg_addr_start>=Float_w_reg_start)&&(Modbus->Reg_addr_start<=Float_w_reg_end))//float
	{
		bit_num=4;
		Modbus->Reg_addr_start=Modbus->Reg_addr_start-Float_w_reg_start;
		*addr_head=(unsigned char *)(&Float_w_reg.head);
		*addr_head=Modbus->Reg_addr_start*2+*addr_head+4;
	}
	else if((Modbus->Reg_addr_start>=LongInt_w_reg_start)&&(Modbus->Reg_addr_start<=LongInt_w_reg_end))//32int
	{
		bit_num=4;
		Modbus->Reg_addr_start=Modbus->Reg_addr_start-LongInt_w_reg_start;
		*addr_head=(unsigned char *)(&LongInt_w_reg.head);
		*addr_head=Modbus->Reg_addr_start*2+*addr_head+4;
	}
	else if((Modbus->Reg_addr_start>=Int_w_reg_start)&&(Modbus->Reg_addr_start<=Int_w_reg_end))//16int
	{
		bit_num=2;
		Modbus->Reg_addr_start=Modbus->Reg_addr_start-Int_w_reg_start;
		*addr_head=(unsigned char *)(&Int_w_reg.head);
		*addr_head=Modbus->Reg_addr_start*2+*addr_head+2;
	}
	else
	{
		bit_num=0;
		Modbus->Error=Modbus_error_reg_addr;
	}
	return bit_num;
}
*/
#ifdef Modbus_slave

void Modbus_Slave_Send_cmd_1_2(Modbus_struct *Modbus,unsigned char cmd)
{
	/*unsigned short 	bit_len,byte_len;
	unsigned char	i,*addr_head;
	Modbus->Reg_addr_start=(((unsigned short)(Modbus->Rx_buff[2]<<8))|((unsigned short)Modbus->Rx_buff[3]));
	Modbus->Reg_num=(((unsigned short)(Modbus->Rx_buff[4]<<8))|((unsigned short)Modbus->Rx_buff[5]));
	byte_len=Modbus->Reg_num/8;
	bit_len=bit_len%8;
	if(bit_len!=0) byte_len=byte_len+1;
	if(cmd==1)
	{
		addr_head=(unsigned char *)(&coils_reg.head);
	}
	else
	{	
		addr_head=(unsigned char *)(&discreteInputs_reg);
	}
	for(i=0;i<byte_len;i++)
	{
		Modbus->Tx_buff[i+3]=*(addr_head+i);
	}
	Modbus->Tx_buff[byte_len-1]=Modbus->Tx_buff[byte_len-1]&(bit_len*2-1);
	Modbus->Tx_buff[2]=byte_len;
	Modbus->Tx_len=byte_len+3;*/
}
void Modbus_Slave_Send_cmd_3_4(Modbus_struct *Modbus,unsigned char cmd)
{
	unsigned char	*addr_head,temp,temp1,bit_num;
	unsigned short  data_len,i;
	if(cmd==3)
	{
		Modbus->Reg_addr_start=(((unsigned short)(Modbus->Rx_buff[2]<<8))|((unsigned short)Modbus->Rx_buff[3]));
	}
	else
	{
		Modbus->Reg_addr_start=(((unsigned short)(Modbus->Rx_buff[2]<<8))|((unsigned short)Modbus->Rx_buff[3]))+9000;
	}
	Modbus->Reg_num=(((unsigned short)(Modbus->Rx_buff[4]<<8))|((unsigned short)Modbus->Rx_buff[5]));
	data_len=Modbus->Reg_num*2;
	Modbus->Tx_buff[2]=data_len;
	Modbus->Tx_len=data_len+3;
	if(Intra_Area(Modbus->Reg_addr_start,other_data_addr,other_Head_end)&&Intra_Area(Modbus->Reg_addr_start+Modbus->Reg_num,other_data_addr,other_Head_end))
	{
		temp=Modbus->Reg_addr_start-other_data_addr;
		for(i=0;i<Modbus->Tx_buff[2];)
		{
			bit_num=computer_addr_head(Modbus,(void *)&addr_head);
			if(bit_num==4)
			{	
				temp=*Modbus->Byte_order_float;
			}
			else if(bit_num==2)
			{	
				temp=*Modbus->Byte_order_16+4;
			}
			for(i=0;i<bit_num;i++)
			{
				Modbus->Tx_buff[i+3]=*(unsigned char *)(addr_head+i+Modbus_byte_order[temp][i%4]);
			}
		}
	}
	else
	{
		bit_num=computer_addr_head(Modbus,(void *)&addr_head);
		if(bit_num==4)
		{	
			temp=*Modbus->Byte_order_float;
		}
		else if(bit_num==2)
		{	
			temp=*Modbus->Byte_order_16+4;
		}
		
		for(temp1=0;i<data_len;i++,temp1++)
		{
			Modbus->Tx_buff[i+3]=*(unsigned char *)(addr_head+i+Modbus_byte_order[temp][i%4]);
		}
	}
}
void Modbus_Slave_Send_cmd_5(Modbus_struct *Modbus)
{
	/*unsigned short 	temp_addr,data;
	unsigned char	*addr_head;
	temp_addr=(((unsigned short)(Modbus->Rx_buff[2]<<8))|((unsigned short)Modbus->Rx_buff[3]));
	data=(((unsigned short)(Modbus->Rx_buff[4]<<8))|((unsigned short)Modbus->Rx_buff[5]));
	addr_head=(unsigned char *)(&coils_reg.head);
	
	Modbus->Tx_buff[2]=Modbus->Rx_buff[2];
	Modbus->Tx_buff[3]=Modbus->Rx_buff[3];
	Modbus->Tx_buff[4]=Modbus->Rx_buff[4];
	Modbus->Tx_buff[5]=Modbus->Rx_buff[5];
	Modbus->Tx_len=6;*/
}
void Modbus_Slave_Send_cmd_6(Modbus_struct *Modbus)
{
	unsigned char	*addr_head,temp;
	unsigned short  data_len,i;
	Modbus->Reg_addr_start=(((unsigned short)(Modbus->Rx_buff[2]<<8))|((unsigned short)Modbus->Rx_buff[3]));
	Modbus->Reg_num=1;
	data_len=Modbus->Reg_num*2;
	computer_addr_head(Modbus,(void *)&addr_head);
	temp=*Modbus->Byte_order_16+4;
	for(i=0;i<data_len;i++)
	{
		*(unsigned char *)(addr_head+i+Modbus_byte_order[temp][i%4])=Modbus->Rx_buff[i+4];
	}
	Modbus->Tx_buff[2]=Modbus->Rx_buff[2];
	Modbus->Tx_buff[3]=Modbus->Rx_buff[3];
	Modbus->Tx_buff[4]=Modbus->Rx_buff[4];
	Modbus->Tx_buff[5]=Modbus->Rx_buff[5];
	Modbus->Tx_len=6;
}
void Modbus_Slave_Send_cmd_16(Modbus_struct *Modbus)
{
	unsigned char	*addr_head,temp,bit_num;
	unsigned char 	i,data_len;
	Modbus->Reg_addr_start=(((unsigned short)(Modbus->Rx_buff[2]<<8))|((unsigned short)Modbus->Rx_buff[3]));
	data_len=Modbus->Rx_buff[6];
	Modbus->Reg_num=data_len/2;
	bit_num=computer_addr_head(Modbus,(void *)&addr_head);
	if(bit_num==4)
	{	
		temp=*Modbus->Byte_order_float;
	}
	else if(bit_num==2)
	{	
		temp=*Modbus->Byte_order_16+4;
	}
	for(i=0;i<data_len;i++)
	{
		*(unsigned char *)(addr_head+i+Modbus_byte_order[temp][i%4])=Modbus->Rx_buff[i+7];
	}
	Modbus->Tx_buff[2]=Modbus->Rx_buff[2];
	Modbus->Tx_buff[3]=Modbus->Rx_buff[3];
	Modbus->Tx_buff[4]=Modbus->Rx_buff[4];
	Modbus->Tx_buff[5]=Modbus->Rx_buff[5];
	Modbus->Tx_len=6;
}
void Modbus_Slave_Send_cmd_20(Modbus_struct *Modbus)
{
	/*unsigned char	request_num,i,a,*addr_head,temp;
	unsigned short 	file_num,addr_num,addr_len;
	request_num=Modbus->Rx_buff[2]/7;
	Modbus->Tx_len=3;
	for(i=0;i<request_num;i++)
	{
		if(Modbus->Rx_buff[i+3]==6)
		{
			temp=i*7;
			file_num=(((unsigned short)(Modbus->Rx_buff[temp+4]<<8))|((unsigned short)Modbus->Rx_buff[temp+5]));
			addr_num=(((unsigned short)(Modbus->Rx_buff[temp+6]<<8))|((unsigned short)Modbus->Rx_buff[temp+7]));
			addr_len=(((unsigned short)(Modbus->Rx_buff[temp+8]<<8))|((unsigned short)Modbus->Rx_buff[temp+9]))*2;
			switch(file_num)
			{
				case 1:
				{
					addr_head=file1;
					break;
				}
				case 2:
				{
					addr_head=file2;
					break;
				}
				default:
    		    {
                    addr_head=0;                    //?????
					Modbus->Error=Modbus_error_reg_addr;
		            break;
				}
			}
			addr_head=addr_head+addr_num;
			Modbus->Tx_buff[Modbus->Tx_len++]=addr_len;
			Modbus->Tx_buff[Modbus->Tx_len++]=6;
			for(a=0;a<addr_len;a++)
			{
				Modbus->Tx_buff[Modbus->Tx_len++]=*(addr_head+a);
			}
		}
	}
	Modbus->Tx_buff[2]=Modbus->Tx_len-5;*/
}
void Modbus_Slave_Send_cmd_21(Modbus_struct *Modbus)
{
	/*unsigned char	request_num,i,a,*addr_head,start_flag;
	unsigned short 	file_num,addr_num,addr_len;
	request_num=Modbus->Rx_buff[2]+3;
	Modbus->Tx_len=3;
	start_flag=1;
	i=3;
	do
	{
		if(start_flag==1)
		{
			if(Modbus->Rx_buff[i]==6)
			{
				file_num=(((unsigned short)(Modbus->Rx_buff[i+1]<<8))|((unsigned short)Modbus->Rx_buff[i+2]));
				addr_num=(((unsigned short)(Modbus->Rx_buff[i+3]<<8))|((unsigned short)Modbus->Rx_buff[i+4]));
				addr_len=(((unsigned short)(Modbus->Rx_buff[i+5]<<8))|((unsigned short)Modbus->Rx_buff[i+6]))*2;
				i=i+7;
                switch(file_num)
				{
					case 1:
					{
						addr_head=file1;
						break;
					}
					case 2:
					{
						addr_head=file2;
						break;
					}
					default:
	    		    {
                        addr_head=0;            //?????
						Modbus->Error=Modbus_error_reg_addr;
			            break;
					}
				}
				addr_head=addr_head+addr_num;
				start_flag=0;
			}
		}
		else
		{
			start_flag=1;
			for(a=0;a<addr_len;a++)
			{
				*(addr_head+a)=Modbus->Tx_buff[i++];
			}
		}
	}while(i<request_num);
	Modbus->Tx_buff[2]=Modbus->Tx_len-5;*/
}

void Modbus_Slave_Send_cmd_23(void)
{
	/*unsigned char	i,*addr_head;
	Modbus->Reg_addr_start=(((unsigned short)(Modbus->Rx_buff[2]<<8))|((unsigned short)Modbus->Rx_buff[3]));
	Modbus->Reg_num=(((unsigned short)(Modbus->Rx_buff[4]<<8))|((unsigned short)Modbus->Rx_buff[5]))*2;
	Modbus->Reg_num=Modbus->Rx_buff[6];
	computer_addr_head(Modbus->Reg_addr_start,(void *)&addr_head);
	for(i=0;i<Modbus->Reg_num;i++)
	{
		Modbus->Tx_buff[i+3]=*(addr_head+i);
	}
	Modbus->Tx_buff[2]=Modbus->Reg_num;
	Modbus->Tx_len=Modbus->Reg_num+3;

	Modbus->Reg_addr_start=(((unsigned short)(Modbus->Rx_buff[6]<<8))|((unsigned short)Modbus->Rx_buff[7]));
	Modbus->Reg_num=Modbus->Rx_buff[10];
	computer_addr_head(Modbus->Reg_addr_start,(void *)&addr_head);
	for(i=0;i<Modbus->Reg_num;i++)
	{
		*(unsigned char *)(addr_head+i)=Modbus->Rx_buff[i+11];
	}*/
}
void Modbus_Slave_Receive_char(Modbus_struct *Modbus,unsigned char input_data)
{
//	HAL_UART_Transmit(&huart1,(uint8_t*)&tmpbuff,1,0xFFFF);
	switch(Modbus->Status)
	{
		case Startus_addr:
		{
			Modbus->Status=Startus_cmd;
			Modbus->Rx_buff[0]=input_data;
			break;	
		}
		case Startus_cmd:
		{
			Modbus->Status=Startus_data;
			Modbus->Rx_buff[1]=input_data;
			Modbus->Rx_len_prt=2;
			break;	
		}
		case Startus_data:
		{			
			Modbus->Rx_buff[Modbus->Rx_len_prt++]=input_data;
			if(Modbus->Rx_len==0)
			{
				switch(Modbus->Rx_buff[1])
				{
					case 16:
					{  
						if(Modbus->Rx_len_prt>=7) 
						{	
							Modbus->Rx_len=Modbus->Rx_buff[6]+9;
						}
						break;
					}
					case 20:
					case 21:
					{
						if(Modbus->Rx_len_prt>=3) 
						{	
							Modbus->Rx_len=Modbus->Rx_buff[2]+5;	
						}
						break;
					}
					case 22:
					{
						Modbus->Rx_len=10;
						break;
					}
					case 23:
					{
						if(Modbus->Rx_len_prt>=11) 
						{	
							Modbus->Rx_len=Modbus->Rx_buff[10]+13;	
						}
						break;
					}
					case 50:
					{
						Modbus->Rx_len=13;
						break;
					}
					default:
    		        {
						Modbus->Rx_len=8;
		            	break;
					}
				}
			}
			if((Modbus->Rx_len_prt>=Modbus->Rx_len)&&(Modbus->Rx_len))
			{
				Modbus->Status=Startus_finished;
			}
		}
	}
}
void Modbus_Slave_Send(Modbus_struct *Modbus)
{
	unsigned short 	tempU16;
	unsigned char	tempU8_a,tempU8_b;
	if((Modbus->Rx_buff[0]!=*Modbus->Addr)&&(Modbus->Rx_buff[0]!=Broadcast_addr))
	{
		Modbus->Status=0;
	}
	else if(Modbus->Status==Startus_finished)
	{
		tempU16=Modbus_CRC(Modbus->Rx_buff,Modbus->Rx_len-2);
		tempU8_a=(unsigned char)(tempU16>>8);
		tempU8_b=(unsigned char)tempU16;
		if((Modbus->Rx_buff[Modbus->Rx_len-1]==tempU8_a)&&(Modbus->Rx_buff[Modbus->Rx_len-2]==tempU8_b))
		{
			Modbus->Cmd=Modbus->Rx_buff[1];
			switch(Modbus->Cmd)
			{
				case 1:
				{
					Modbus_Slave_Send_cmd_1_2(Modbus,1);
					break;
				}
				case 2:
				{
					Modbus_Slave_Send_cmd_1_2(Modbus,2);
					break;
				}
				case 3:
				{
					Modbus_Slave_Send_cmd_3_4(Modbus,3);
					break;
				}
				case 4:
				{
					Modbus_Slave_Send_cmd_3_4(Modbus,4);
					break;
				}
				case 5:
				{
					Modbus_Slave_Send_cmd_5(Modbus);
					break;
				}
				case 6:
				{
					Modbus_Slave_Send_cmd_6(Modbus);
					break;
				}
				case 16:
				{
					Modbus_Slave_Send_cmd_16(Modbus);
					break;
				}
				case 20:
				{
					//Modbus_Slave_Send_cmd_20();
					break;
				}
				case 21:
				{
					//Modbus_Slave_Send_cmd_21();
					break;
				}
				case 23:
				{
					//Modbus_Slave_Send_cmd_23();
					break;
				}
				default:
	    		{
					Modbus->Error=Modbus_error_cmd;
			       	break;
				}
			}
		}
		else
		{
			Modbus->Error=Modbus_error_crc;
		}
		if(Modbus->Error!=0)
		{
			Modbus->Tx_buff[0]=*Modbus->Addr;
			Modbus->Tx_buff[1]=(Modbus->Rx_buff[1]|0x80);
			Modbus->Tx_buff[2]=Modbus->Error;
			Modbus->Tx_len=3;
		}
		else
		{
			Modbus->Tx_buff[0]=*Modbus->Addr;
			Modbus->Tx_buff[1]=Modbus->Rx_buff[1];
		}
		if(Modbus->Tx_len!=0)
		{
			tempU16=Modbus_CRC(Modbus->Tx_buff,Modbus->Tx_len);
            Modbus->Tx_buff[Modbus->Tx_len++]=(unsigned char)(tempU16);
            Modbus->Tx_buff[Modbus->Tx_len++]=(unsigned char)(tempU16>>8);
		}
		Modbus->Status=0;
		Modbus->Rx_len=0;
		Modbus->Rx_len_prt=0;
		Modbus->Error=0;
	}
}
#endif 

#ifdef Modbus_master
void Modbus_Master_Send_cmd_1_2(Modbus_struct *Modbus,unsigned char cmd)
{
	
}
void Modbus_Master_Send_cmd_3_4(Modbus_struct *Modbus)
{
	Modbus->Tx_buff[1]=Modbus->Cmd;
	Modbus->Tx_buff[2]=(unsigned char)((Modbus->Reg_addr_start)>>8);
	Modbus->Tx_buff[3]=(unsigned char)(Modbus->Reg_addr_start);
	Modbus->Tx_buff[4]=(unsigned char)((Modbus->Reg_num)>>8);
	Modbus->Tx_buff[5]=(unsigned char)(Modbus->Reg_num);
	Modbus->Tx_len=6;

	Modbus->Rx_num=Modbus->Reg_num+5;
}
void Modbus_Master_Send_cmd_5(Modbus_struct *Modbus)
{
	
}

void Modbus_Master_Send_cmd_6(Modbus_struct *Modbus)
{
/*
	unsigned char bit_num,*addr_head;
	Modbus->Reg_addr_start=Modbus->Reg_addr_start-1;
	Modbus->Tx_buff[1]=Modbus->Cmd;
	Modbus->Tx_buff[2]=(unsigned char)((Modbus->Reg_addr_start)>>8);
	Modbus->Tx_buff[3]=(unsigned char)(Modbus->Reg_addr_start);

	bit_num=computer_addr_head(Modbus,(void *)&addr_head);
	Byte_order(addr_head,&Modbus->Tx_buff[4],2,*Modbus->Byte_order_16+4);
	Modbus->Tx_len=6;

	Modbus->Rx_num=8;
	*/
	Modbus->Tx_buff[1]=Modbus->Cmd;
	Modbus->Tx_buff[2]=(unsigned char)((Modbus->Reg_addr_start)>>8);
	Modbus->Tx_buff[3]=(unsigned char)(Modbus->Reg_addr_start);
	Modbus->Tx_buff[4]=(unsigned char)((Modbus->Reg_num));
	Modbus->Tx_buff[5]=(unsigned char)(Modbus->Reg_num>>8);
	Modbus->Tx_len=6;

	Modbus->Rx_num=8;
}
void Modbus_Master_Send_cmd_16(Modbus_struct *Modbus)
{
	unsigned char bit_num,*addr_head,temp;
//	unsigned short addr;
	
	Modbus->Reg_addr_start=Modbus->Reg_addr_start-1;
	
	Modbus->Tx_buff[1]=(unsigned char)Modbus->Cmd;
	Modbus->Tx_buff[2]=(unsigned char)((Modbus->Reg_addr_start)>>8);
	Modbus->Tx_buff[3]=(unsigned char)(Modbus->Reg_addr_start);
	Modbus->Tx_buff[4]=(unsigned char)((Modbus->Reg_num)>>8);
	Modbus->Tx_buff[5]=(unsigned char)(Modbus->Reg_num);
	Modbus->Tx_buff[6]=(unsigned char)(Modbus->Reg_num*2);
	/*bit_num=computer_addr_head(Modbus,(void *)&addr_head);
	if(bit_num==4)
	{	
		temp=*Modbus->Byte_order_float;
	}
	else if(bit_num==2)
	{	
		temp=*Modbus->Byte_order_16+4;
	}*/
	//Byte_order(addr_head,&Modbus->Tx_buff[7],Modbus->Tx_buff[6],temp);
	Modbus->Tx_len=6+Modbus->Tx_buff[6]+1;
	
	Modbus->Rx_num=8;
}
void Modbus_Master_Send_cmd_20(Modbus_struct *Modbus)
{
	
}
void Modbus_Master_Send_cmd_21(Modbus_struct *Modbus)
{
	
}

void Modbus_Master_Send_cmd_23(void)
{
	
}
void Modbus_Master_Receive_cmd_1_2(Modbus_struct *Modbus,unsigned char cmd)
{
	
}
void Modbus_Master_Receive_cmd_3_4(Modbus_struct *Modbus,unsigned char cmd)
{
	unsigned short  i;
	for(i=0;i<Modbus->Rx_buff[2];i++)
	{
		Modbus->Rx_data[i]=Modbus->Rx_buff[3+i];
	}
}
void Modbus_Master_Receive_cmd_5(Modbus_struct *Modbus)
{
	
}
void Modbus_Master_Receive_cmd_6(Modbus_struct *Modbus)
{/*
	unsigned char	*addr_head,temp,bit_num;
	bit_num=computer_addr_head(Modbus,(void *)&addr_head);
	temp=*Modbus->Byte_order_16+4;
	Byte_order(&Modbus->Rx_buff[2],addr_head,bit_num,temp);
	*/
}
void Modbus_Master_Receive_cmd_16(Modbus_struct *Modbus)
{
	
}
void Modbus_Master_Receive_cmd_20(Modbus_struct *Modbus)
{
	
}
void Modbus_Master_Receive_cmd_21(Modbus_struct *Modbus)
{
	
}

void Modbus_Master_Receive_cmd_23(void)
{
	
}
void Modbus_Master_Send(Modbus_struct *Modbus)
{
	unsigned short temp16;
	if(Modbus->Status_send==Modbus_Start_send)
	{
		Modbus->Send_flag=1;
		switch(Modbus->Cmd)
		{
			case 3:
			{
				Modbus_Master_Send_cmd_3_4(Modbus);
				break;
			}
			case 4:
			{
				Modbus_Master_Send_cmd_3_4(Modbus);
				break;
			}
			case 6:
			{
				Modbus_Master_Send_cmd_6(Modbus);
				break;
			}
			case 16:
			{
				Modbus_Master_Send_cmd_16(Modbus);
				break;
			}
			default:
		    {
				break;
			}
		}
		Modbus->Tx_buff[0]=*Modbus->Addr;
		temp16=Modbus_CRC(Modbus->Tx_buff,Modbus->Tx_len);
	    Modbus->Tx_buff[Modbus->Tx_len++]=(unsigned char)(temp16);
	    Modbus->Tx_buff[Modbus->Tx_len++]=(unsigned char)(temp16>>8);
	}
}

void Modbus_Master_Receive(Modbus_struct *Modbus)
{
	unsigned short 	tempU16;
	unsigned char	tempU8_a,tempU8_b;
	if(Modbus->Rx_buff[0]==*Modbus->Addr)
	{
		tempU16=Modbus_CRC(Modbus->Rx_buff,Modbus->Rx_len-2);
		tempU8_a=(unsigned char)(tempU16>>8);
		tempU8_b=(unsigned char)tempU16;
		if((Modbus->Rx_buff[Modbus->Rx_len-1]==tempU8_a)&&(Modbus->Rx_buff[Modbus->Rx_len-2]==tempU8_b))
		{
			Modbus->Cmd=Modbus->Rx_buff[1];
			switch(Modbus->Cmd)
			{
				case 3:
				{
					Modbus_Master_Receive_cmd_3_4(Modbus,3);
					break;
				}
				case 4:
				{
					Modbus_Master_Receive_cmd_3_4(Modbus,4);
					break;
				}
				case 6:
				{
					//Modbus_Master_Receive_cmd_6(Modbus);
					break;
				}
				case 16:
				{
					Modbus_Master_Receive_cmd_16(Modbus);
					break;
				} 
				default:
	    		{
					Modbus->Error=Modbus_error_cmd;
			       	break;
				}
			}
		}
		else
		{
			Modbus->Error=Modbus_error_crc;
		}
	}
	else
	{
		Modbus->Error=Modbus_error_addr;
	}
}
void Modbus_Master_Receive_char(Modbus_struct *Modbus,unsigned char input_data)
{
	Modbus->Rx_buff[Modbus->Rx_len++]=input_data;
	if(Modbus->Rx_len>=Modbus->Rx_num)
	{
		Modbus->Status_send=Modbus_RX_OK_send;
		Modbus_Master_Receive(Modbus);
	}
}

void Modbus_Master_deal(Modbus_struct *Modbus)
{
	switch(Modbus->Status_send)
	{
		case Modbus_Start_send:
		{
			Modbus_Master_Send(Modbus);
			Modbus->Status_send=Modbus_TX_OK_send;
			Modbus->Rx_len=0;
			break;
		}
		case Modbus_RX_OK_send:
		{
			Modbus_Master_Receive(Modbus);
			if(Modbus->Error!=Modbus_error_null)
			{
				Modbus->Status_send=Modbus_Start_send;//???
			}
			else
			{
				Modbus->Status_send=Modbus_Ready_send;
			}
			break;
		}
		default:
	    {
			Modbus->Status_send=Modbus_Ready_send;
			break;
		}
	}
}

void Modbus_Master_Read_Data(Modbus_struct *Modbus,unsigned char address,unsigned short reg,unsigned char *buf,unsigned short num)
{
	Modbus->Status_send=Modbus_Start_send;
	Modbus->Cmd=3;						
	Modbus->Reg_addr_start=reg-1;			
	Modbus->Reg_num=num;
	Modbus->Rx_data=buf;
	Modbus_Master_deal(Modbus);
}
void Modbus_Master_Write_Data(Modbus_struct *Modbus,unsigned short reg,unsigned short num)
{
	Modbus->Status_send=Modbus_Start_send;
	Modbus->Cmd=6;						
	Modbus->Reg_addr_start=reg-1;			
	Modbus->Reg_num=num;
	Modbus_Master_deal(Modbus);
}
void Modbus_Master_Write_2Data(Modbus_struct *Modbus,unsigned short reg,u32 num)
{
	Modbus->Status_send=Modbus_Start_send;
	Modbus->Cmd=16;						
	Modbus->Reg_addr_start=reg;			
	Modbus->Reg_num=2;
	Modbus->Tx_buff[7]=(unsigned char)num;
	Modbus->Tx_buff[8]=(unsigned char)(num>>8);
	Modbus->Tx_buff[9]=(unsigned char)(num>>16);
	Modbus->Tx_buff[10]=(unsigned char)(num>>24);
	Modbus_Master_deal(Modbus);
}
#endif 
